package de.tabuma.sso.group;

import de.tabuma.sso.*;
import de.tabuma.sso.sso.*;
import de.tabuma.sso.user.*;
import de.tabuma.sso.useruserrelation.*;
import de.tabuma.sso.usergroup.*;
import de.tabuma.shopping.logik.*;
import java.util.*;
import java.util.logging.*;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1.1.1.2.1.2.1 $
 * @since $Date: 2010/01/13 10:23:10 $
 */
public class GroupWebservice {

    private static final Logger log = Logger.getLogger(GroupWebservice.class.getName());

    public static int create(Group group, String ssoId) throws Exception {
        int groupId = GroupWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUP_CREATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        group.ableToCreateGroupWithNoLicenseNeeded = false;
        if(group.noLicenseNeeded) {
            if(!GroupWebserviceLib.userIsMemberOfGroupWithAbilityToCreatedNoLicenseNeedGroup(user.getId().intValue())) {
                group.noLicenseNeeded = false;
            }
        }
        if(group.adminAll) {
            group.adminAll = false;
        }


        GroupWebserviceLib.userHasPrivilegForGroup(group, ssoId);
        return GroupFacade.create(group, user.getName());
    }

    public static void update(Group group, String ssoId) throws Exception {
        int groupId = GroupWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUP_UPDATE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Group updateGroup = GroupFacade.get(group.getId().intValue());
        GroupWebserviceLib.userHasPrivilegForGroup(updateGroup, ssoId);
        GroupWebserviceLib.userHasPrivilegForGroup(group, ssoId);
        GroupFacade.update(group, user.getName());
    }

    public static Group get(int id, String ssoId) throws Exception {
        int groupId = GroupWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUP_GET, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Group group = GroupFacade.get(id);
        GroupWebserviceLib.userHasPrivilegForGroup(group, ssoId);
        return group;
    }

    public static void remove(int id, String ssoId) throws Exception {
        int groupId = GroupWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUP_REMOVE, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }

        Group group = GroupFacade.get(id);
        
        GroupWebserviceLib.userHasPrivilegForGroup(group, ssoId);
        
        GroupFacade.remove(id, user.getName());
    }

    public static List<Group> getList(String ssoId) throws Exception {
        int groupId = GroupWebserviceLib.userHasPrivileg(ssoId);
        SSO sso = SSOFacade.get(ssoId);
        User user = UserFacade.get(sso.getUserId());
        Group g1 = GroupFacade.get(groupId);
        if(!g1.getNoLicenseNeeded().booleanValue()) {
            LicenseUseFacade.create(groupId, user.getName() + "/" + g1.getName(), constants.GROUP_GETLIST, constants.SSO_SOAP_LICENSE_TYP, user.getName());
        }
        
        List<UserUserRelation> uurList = UserUserRelationFacade.getList(sso.getUserId());
        List<Group> gList = new ArrayList();
        for(int i=0;i<uurList.size();i++) {
            gList.addAll(GroupFacade.getList(uurList.get(i).getUser()));
        }

        List<UserGroup> ugList = UserGroupFacade.getList1(sso.getUserId());
        for(int i=0;i<ugList.size();i++) {
            UserGroup ug = ugList.get(i);
            for(int j=0;j<gList.size();j++) {
                Group group = gList.get(j);
                if(group.getId().intValue()==ug.getGroupId()) {
                    gList.remove(j);
                    j--;
                }
            }
        }
        return gList;
    }
}